PROJ_BASE := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
ARCH:=$(shell uname -m)
PLATFORM:=$(shell uname -s)
KERNEL_VER:=$(shell uname -r)
REDIS_SER_VER:=$(shell redis-server --version)
REDIS_PORT?=6379
REDIS_PASS?=none
REDIS_VALUE_SIZE:=10 20 50 100 200 1000 5000


ifneq ($(REDIS_PASS), none)
REDIS_FLAG=-a $(REDIS_PASS)
endif

.PHONY: test-get-set
test-get-set:
	@echo "Redis version:" $(REDIS_SER_VER)
	$(call platform_info)
	@echo "\ntesting redis-benchmark GET/SET..."
	@$(foreach size,$(REDIS_VALUE_SIZE), \
		$(call redis_bench_get_set, $(size));\
	)

.PHONY: test-mem-usage
test-mem-usage:
	@echo "Redis version:" $(REDIS_SER_VER)
	$(call platform_info)
	@echo "\ntesting memory usage per key..."
	@echo $(shell perl -E 'say "-" x 84')
	@go run $(PROJ_BASE) -p $(REDIS_PORT) $(REDIS_FLAG)

define platform_info
	@echo "Platform:" $(PLATFORM)
	@echo "Arch:" $(ARCH)
	@echo "Kernel release version:" $(KERNEL_VER)
endef
define redis_bench_get_set
	echo "testing get/set with value size" $(1)
	echo "------------------------------------------"
	redis-benchmark -p $(REDIS_PORT) $(REDIS_FLAG) -q -t GET,SET -d $(1)
endef